package cn.mw.cmdb.trigger;

import cn.mw.cmdb.entity.PropertyTypeList;
import cn.mw.cmdb.entity.PropertyTypeString;
import cn.mw.components.mongodb.anno.AutoId;
import cn.mw.components.mongodb.anno.CMDBProperty;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.StringUtils;

import java.util.List;

@Data
@Document("mw_cmdbmd_notify")
public class MwCmdbNotify {

    // ID字段
    @CMDBProperty(searchType = PropertyTypeString.ID, searchPrefix = "_")
    @AutoId
    private String id;

    @CMDBProperty(searchType = PropertyTypeString.ID, keywordSearch = true)
    private String title;

    private Integer beforeNum;

    private PropertyTypeTimeUnit unit;

    private String keyword;

    @CMDBProperty(searchType = PropertyTypeString.ID)
    private String triggerName;

    //范围
    private MwNofityScope scope;

    //提醒规则
    private MwNotifyRule notifyRule;

    //执行类型
    private MwNotifyExeType exeType;

    //执行时间计划id
    private String timePlanId;

    //定时任务id
    @CMDBProperty(searchType = PropertyTypeList.ID)
    private List<Long> taskIds;

    //消息发送规则
    private List<MwMessageRule> messageRules;

    //上一次提醒的数量
    @CMDBProperty
    private Integer notifyCount = 0;

    @CMDBProperty(searchType = PropertyTypeList.ID)
    private List<Long> groups;

    @CMDBProperty(searchType = PropertyTypeList.ID)
    private List<List<Long>> orgs;

    public boolean compareTimePlanId(MwCmdbNotify mwCmdbNotify) {
        boolean src = StringUtils.hasText(timePlanId);
        boolean dest = StringUtils.hasText(mwCmdbNotify.getTimePlanId());
        if (src && dest) {
            return timePlanId.equals(mwCmdbNotify.getTimePlanId());
        }

        if (!src || !dest) {
            return false;
        }

        return true;
    }

    public void extractFrom(MwCmdbNotifyParam param){
        if(null != param.getScopeParam() && !param.getScopeParam().isEmpty()){
            scope = new MwNofityScope();
            for(MwCmdbNotifyScopeParam item : param.getScopeParam()){
                switch (item.getType()){
                    case modelGroup -> scope.addModelGroupId(item.getId());
                    case model -> scope.addModelId(item.getId());
                    case instance -> scope.addInstanceId(item.getId());
                }
            }
        }
    }

}
